global input "Q:\dc1prhcmsas01\PU2\data - sas"
global temp "Q:\dc1prhcmsas01\PU2\temp_stata_wr"
global log "Q:\dc1prhcmsas01\PU2\Log - Stata WR"
global output "Q:\dc1prhcmsas01\PU2\data_stata_wr"
global graphs "Q:\dc1prhcmsas01\PU2\graphs_stata_wr"
global tables "Q:\dc1prhcmsas01\PU2\tables_stata_wr"

cd "Q:\dc1prhcmsas01\PU2\Code - Stata WR"
set scheme plotplain
graph set window fontface "Times New Roman"

capture log close
log using "$log/6_staggered_bargaining", replace

*********************************************************
*Fig B1: Number of Wage Changers per Year, by worker
*********************************************************
	use yr_month client_id state emp_pur_c salaried base ot base0 using "$output\stayers_panel_treat.dta", clear
	keep if yr_month>=mdy(5,1,2015) & yr_month<=mdy(5,1,2016) & salaried==1
	bysort client_id state emp_pur_c (yr_month): gen diff_base=base-base[_n-1]
	 
	gen wage_change=diff_base!=0 & diff_base!=.
	collapse (sum) num=wage_change, by(client_id state emp_pur_c)
	histogram num, width(1) percent xtitle("Number of Wage Changes") ytitle("Share of Workers") xlabel(0(1)12) discrete
	graph export "$graphs/figB1_number_of_changes.eps", replace
	graph export "$graphs/figB1_number_of_changes.jpg", replace
	
*********************************************************
*Fig B2: Months since last pay raise
*********************************************************
	use yr_month client_id state emp_pur_c salaried base ot base0 using "$output\stayers_panel_treat.dta", clear
	keep if yr_month>=mdy(5,1,2015) & yr_month<=mdy(5,1,2018) & salaried==1
	bysort client_id state emp_pur_c (yr_month): gen diff_base=base-base[_n-1]
	keep if diff_base!=0 & diff_base!=.
	bysort client_id state emp_pur_c (yr_month): gen diff_months=mofd(yr_month)-mofd(yr_month[_n-1])
	
	histogram diff_months, width(1) percent xtitle("Months Since Last Pay Increase") ytitle("Share of Pay Increases") xlabel(0(1)36) discrete
	graph export "$graphs/figB2_months_since_increase.eps", replace
	graph export "$graphs/figB2_months_since_increase.jpg", replace 
	
	
*********************************************************
*Fig B3: Month of pay increase
*********************************************************
	*Worker
	use yr_month client_id state emp_pur_c salaried base ot base0 using "$output\stayers_panel_treat.dta", clear
	keep if yr_month>=mdy(5,1,2015) & yr_month<=mdy(5,1,2016) & salaried==1
	bysort client_id state emp_pur_c (yr_month): gen diff_base=base-base[_n-1]
	gen month=month(yr_month) if diff_base!=0 & diff_base!=.
	
	label define time_lab 1 "Jan" 2 "Feb" 3 "Mar" 4 "Apr" 5 "May" 6 "Jun" 7 "Jul" 8 "Aug" 9 "Sep" 10 "Oct" 11 "Nov" 12 "Dec", replace
	label values month time_lab 
	
	histogram month, width(1) percent xtitle("Month of Wage Change") ytitle("Share of Workers") xlabel(1(1)12, valuelabel angle(45)) discrete
	graph export "$graphs/figB3_month_of_increase_worker.eps", replace
	graph export "$graphs/figB3_month_of_increase_worker.jpg", replace
	
	*Firm
	gen wage_change=diff_base!=0 & diff_base!=.
	collapse (sum) num=wage_change, by(client_id yr_month)
	egen max_num=max(num), by(client_id) 
	gen month=month(yr_month)
	gen mode_month = month if num==max_num
	label values mode_month time_lab 
	histogram mode_month, width(1) percent xtitle("Month of Wage Change") ytitle("Share of Firms") xlabel(1(1)12, valuelabel angle(45)) discrete
	graph export "$graphs/figB3_month_of_increase_firm.eps", replace
	graph export "$graphs/figB3_month_of_increase_firm.jpg", replace
	

**********************************************************************
*Fig B4: Share of workers bunched by month of last pay increase
**********************************************************************
	use "$temp/staggered_bargaining.dta", clear
		/*Note: You'll get the warning "Warning:  variance matrix is nonsymmetric or highly singular". 
		The reason is that in many instances, there is zero variation in the outcome before the "last increase" month.
		That is, workers receive a pay increase only once per year, so once you condition on last_increase, 
		you get identical estimates for the first X months before the first pay increase."*/
	
forvalues m=1/12 {
	preserve 
		keep if last_increase==`m'

	*Calculate diff-in-diff by month 
	forvalues i=1/37 {
		gen d`i'=treat*(time==`i'-12)
	}
	drop d12
	gen event_time=.
	gen beta=.
	
	reghdfe share d*, absorb(treat time) nocon vce(cluster client_id)
	forvalues i=1/37 {
		replace event_time=`i'-12 if _n==`i'
		
		if `i'!=12 {
		replace beta=_b[d`i'] if _n==`i'
		}
		if `i'==12 {
		replace beta=0 if _n==`i'
		}
	}
	keep if beta!=.
	keep event_time beta
	
	*Save estimates
		label define time_lab -12 "Apr 2015" -11 "May 2015" -10 "Jun 2015" ///
			-9 "Jul 2015" -8 "Aug 2015" -7 "Sep 2015" -6 "Oct 2015" -5 "Nov 2015" -4 "Dec 2015" ///
			-3 "Jan 2016" -2 "Feb 2016" -1 "Mar 2016" 0 "Apr 2016" 1 "May 2016" 2 "Jun 2016" ///
			3 "Jul 2016" 4 "Aug 2016" 5 "Sep 2016" 6 "Oct 2016" 7 "Nov 2016" 8 "Dec 2016" ///
			9 "Jan 2017" 10 "Feb 2017" 11 "Mar 2017" 12 "Apr 2017" 13 "May 2017" 14 "Jun 2017" ///
			15 "Jul 2017" 16 "Aug 2017" 17 "Sep 2017" 18 "Oct 2017" 19 "Nov 2017" 20 "Dec 2017" ///
			21 "Jan 2018" 22 "Feb 2018" 23 "Mar 2018" 24 "Apr 2018" 25 "May 2018", replace
		label values event_time time_lab 
		gen m=`m'
		
		save "$output/figB4_estimates_month`m'.dta", replace
	restore 
}

use "$output/figB4_estimates_month1.dta", clear
forvalues m=2/12{
	append using "$output/figB4_estimates_month`m'.dta"
}
	*Plot 
	preserve 
		keep if m<=6
		graph twoway (connect beta event_time if m==1, lc(black) mcolor(black)) (connect beta event_time if m==2) ///
			(connect beta event_time if m==3) (connect beta event_time if m==4) (connect beta event_time if m==5) ///
			(connect beta event_time if m==6), ///
			xline(0 8, lcolor(black)) xline(15, lcolor(red)) xtitle("Month") xlabel(-12(3)25, valuelabel angle(45)) ///
			yline(0) ytitle("Effect on Share of Salaried Workers" "Earning $913/Week") ylabel(-0.005(0.005)0.025)  ///
			legend(order(1 "Jan" 2 "Feb" 3 "Mar" 4 "Apr" 5 "May" 6 "Jun") ring(0) position(2) col(1) region(lstyle(solid) lcolor(black) lwidth(thin)))
		graph export "$graphs/figB4_bunch_mass_months0.eps", replace
		graph export "$graphs/figB4_bunch_mass_months0.jpg", replace
	restore 
		
	preserve 
		keep if m>6
		graph twoway (connect beta event_time if m==7, lc(black) mcolor(black)) (connect beta event_time if m==8) ///
			(connect beta event_time if m==9) (connect beta event_time if m==10) (connect beta event_time if m==11) ///
			(connect beta event_time if m==12), ///
			xline(0 8, lcolor(black)) xline(15, lcolor(red)) xtitle("Month") xlabel(-12(3)25, valuelabel angle(45)) ///
			yline(0) ytitle("Effect on Share of Salaried Workers" "Earning $913/Week") ylabel(-0.005(0.005)0.025)  ///
			legend(order(1 "Jul" 2 "Aug" 3 "Sep" 4 "Oct" 5 "Nov" 6 "Dec") ring(0) position(2) col(1) region(lstyle(solid) lcolor(black) lwidth(thin)))
		graph export "$graphs/figB4_bunch_mass_months1.eps", replace
		graph export "$graphs/figB4_bunch_mass_months1.jpg", replace
	restore 
		

**********************************************************************
*Fig B5: Share of workers bunched by firm size
**********************************************************************
	*Calculate share in treated, collapse by date, difference relative to April 2016
	use "$output\stayers_distribution_treat.dta", clear
	keep if salaried==1
	replace bin=floor((bin-913)/40)*40+913
	collapse (sum) n, by(yr_month client_id bin)
	bysort yr_month client_id: egen emp=sum(n)
	keep if bin==913	
	gen share=n/emp
	gen treat=1
	gen time=mofd(yr_month)-mofd(mdy(4,01,2016))
		*Calculate median firm size in base period 
		preserve 
		keep if yr_month==mdy(4,1,2016)
		egen median=median(emp)
		gen large=emp>=median
		keep client_id large
		tempfile temp 
		save `temp'
		restore 
		merge m:1 client_id using `temp'
		drop _merge
	tempfile treat
	save `treat'
	
	*Calculate share in control, collapse by date, difference relative to April 2014
	use "$output\stayers_distribution_control.dta", clear
	keep if salaried==1
	replace bin=floor((bin-913)/40)*40+913
	collapse (sum) n, by(yr_month client_id bin)
	bysort yr_month client_id: egen emp=sum(n)
	keep if bin==913 
	gen share=n/emp
	gen treat=0
	gen time=mofd(yr_month)-mofd(mdy(4,01,2014))
		*Calculate median firm size in base period 
		preserve 
		keep if yr_month==mdy(4,1,2014)
		egen median=median(emp)
		gen large=emp>=median
		keep client_id large
		tempfile temp 
		save `temp'
		restore 
		merge m:1 client_id using `temp'
		drop _merge
	append using `treat'
	
*Program to estimate and plot bunching mass over time, given firm size
cap program drop plot_connect
program define plot_connect
args size

	preserve 
		keep if large==`size'

	*Calculate diff-in-diff by month 
	forvalues i=1/37 {
		gen d`i'=treat*(time==`i'-12)
	}
	drop d12
	gen event_time=.
	gen beta=.
	gen lb=.
	gen ub=.
	
	reghdfe share d*, absorb(treat time) nocon vce(cluster client_id)
	forvalues i=1/37 {
		replace event_time=`i'-12 if _n==`i'
		
		if `i'!=12 {
		replace beta=_b[d`i'] if _n==`i'
		replace ub = _b[d`i']+1.96*_se[d`i'] if _n==`i'
		replace lb = _b[d`i']-1.96*_se[d`i'] if _n==`i'
		}
		if `i'==12 {
		replace beta=0 if _n==`i'
		replace ub =0 if _n==`i'
		replace lb =0 if _n==`i'
		}
	}
	keep if beta!=.
	keep event_time beta ub lb	
	
	*Save estimates
		save "$output/figB5_estimates`size'.dta", replace

		label define time_lab -12 "Apr 2015" -11 "May 2015" -10 "Jun 2015" ///
			-9 "Jul 2015" -8 "Aug 2015" -7 "Sep 2015" -6 "Oct 2015" -5 "Nov 2015" -4 "Dec 2015" ///
			-3 "Jan 2016" -2 "Feb 2016" -1 "Mar 2016" 0 "Apr 2016" 1 "May 2016" 2 "Jun 2016" ///
			3 "Jul 2016" 4 "Aug 2016" 5 "Sep 2016" 6 "Oct 2016" 7 "Nov 2016" 8 "Dec 2016" ///
			9 "Jan 2017" 10 "Feb 2017" 11 "Mar 2017" 12 "Apr 2017" 13 "May 2017" 14 "Jun 2017" ///
			15 "Jul 2017" 16 "Aug 2017" 17 "Sep 2017" 18 "Oct 2017" 19 "Nov 2017" 20 "Dec 2017" ///
			21 "Jan 2018" 22 "Feb 2018" 23 "Mar 2018" 24 "Apr 2018" 25 "May 2018", replace
		label values event_time time_lab 
	
	*Plot 
		graph twoway (connect beta event_time, lc(black) mcolor(black)) (rcap ub lb event_time, color(black)), ///
			xline(0 8, lcolor(black)) xline(15, lcolor(red)) xtitle("Month") xlabel(-12(3)25, valuelabel angle(45)) ///
			yline(0) ytitle("Effect on Share of Salaried Workers" "Earning $913/Week") ylabel(-0.0025(0.0025)0.015)  legend(off)
		graph export "$graphs/figB5_bunch_mass_size`size'.eps", replace
		graph export "$graphs/figB5_bunch_mass_size`size'.jpg", replace
		
	restore 
end 

plot_connect 1
plot_connect 0

log close
